package com.xxin.controller.sys;

import com.xxin.pojo.entity.SysRoles;
import com.xxin.pojo.vo.MenuVO;
import com.xxin.resp.Result;
import com.xxin.service.SysRoleService;
import com.xxin.service.SysUsersService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 小欣
 * @version 1.0
 * @since 1.8
 */
@Controller
@RequestMapping("/api/auth")
public class SysRoleController {
	
	private final Logger log = LogManager.getLogger(SysRoleController.class);
	
	@Value("${current.user.sessionKey}")
	private String currentUserSessionKey;
	
	@Resource
	private SysUsersService sysUsersService;
	
	@Resource
	private SysRoleService sysRoleService;
	
	
	/*
	退出登录
	 */
	@GetMapping("/logout")
	public String logout(HttpSession session){
		log.info("退出登录，清空session，Key为:{}",currentUserSessionKey);
		session.removeAttribute(currentUserSessionKey);
		return "redirect:/";
	}
	
	/**
	 * 获取菜单列表
	 * @param userId 根据用户编号获取菜单列表
	 */
	@GetMapping("/getMenuList.json")
	@ResponseBody
	public Result<List<MenuVO>> getMenuList(@RequestParam("userId") Integer userId){
		log.info("菜单列表查询，用户编号为:{}",userId);
		List<MenuVO> list = sysUsersService.getMenuList(userId);
		return Result.success(list);
	}
	
	/**
	 * 获取角色信息列表
	 */
	@GetMapping("/getRoleList.json")
	@ResponseBody
	public Result<List<SysRoles>> getRoleList(){
		List<SysRoles> roleList = sysRoleService.getRoleList();
		return Result.success(roleList);
	}
}
